package utils;


/**
 * @author jiangfeng on 2023/4/10
 */
public class ListNodeUtils {

    /* // for copy
    public static void printLink(LinkNode head) {
        while (head != null) {
            System.out.print(head.value + "->");
            head = head.next;
        }
        System.out.println("");
    }
    */

    /* // for copy

     */
    public static class ListNode {
        public int val;
        public ListNode next;

        public ListNode() {
        }

        public ListNode(int val) {
            this.val = val;
        }

        public ListNode(ListNode next) {
            this.next = next;
        }

        public ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }

        public ListNode(int[] n) {
            this.val = n[0];
            ListNode preNode = this;
            for (int i : n) {
                ListNode node = new ListNode(i);
                preNode.next = node;
                preNode = node;
            }
        }

        @Override
        public String toString() {
            String nextValue = next != null ? String.valueOf(next.val) : "";
            return val + "->" + nextValue;
        }
    }


    public static void printLink(ListNode head) {
        while (head != null) {
            System.out.print(head.val + "->");
            head = head.next;
        }
        System.out.printf("\n");
    }


    public static ListNode createLink(int... n) {
        if (n.length == 1) {
            return new ListNode(n[0]);
        }
        ListNode head = new ListNode(n[0]);
        ListNode preNode = head;
        for (int i = 1; i < n.length; i++) {
            ListNode node = new ListNode(n[i]);
            preNode.next = node;
            preNode = node;
        }
        return head;
    }

    public static ListNode createLinkSize(int n) {
        if (n < 2) {
            return null;
        }
        ListNode head = new ListNode(1);
        ListNode preNode = head;
        for (int i = 2; i <= n; i++) {
            ListNode node = new ListNode(i);
            preNode.next = node;
            preNode = node;
        }
        return head;
    }

}
